package ezCall;

import net.rim.blackberry.api.invoke.Invoke;
import net.rim.blackberry.api.invoke.PhoneArguments;
import net.rim.device.api.i18n.Locale;
import net.rim.device.api.ui.Keypad;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.Dialog;


public class PhoneList_PopupScreen  extends List_PopupScreen 
{

	boolean _AfterCalling;
	boolean _AfterEmail;
	boolean _AfterSMS;
	boolean _Selected;
	
	boolean _Action;
	
	private static Option_Model     _Option_Model;
	
	
	public PhoneList_PopupScreen(String[] StrArray) {
		super(StrArray);
		// TODO Auto-generated constructor stub
		
		_AfterCalling = false;
		_Selected = false;
		_AfterSMS = false;
		
		_Action = false;
	}

	public PhoneList_PopupScreen( String[] StrHeaderArray, String[] StrArray )
    {   
		super(StrHeaderArray, StrArray);

		int MobileNumberIndex = Contact_PhoneNum.Get_MobileNumberIndex( StrArray );
		
		if( MobileNumberIndex >= 0 )
		{
			super.Set_ListSelectedIndex(MobileNumberIndex);
		}
		
		_AfterCalling = false;
		_Selected = false;
		_AfterSMS = false;
    }
	
	public void Set_OptionModel( Option_Model _Option_Model_Arg )
	{
		_Option_Model = _Option_Model_Arg;
	}
	
	public boolean keyDown(int keycode, int time)
	{
		int SelectedListIndex;
		boolean Ret = false;
		
		final int KEYCODE_E = 0x45;
		final int KEYCODE_S = 0x53;
		final int KEYCODE_M = 0x4D;
		final int KEYCODE_N = 0x4E;
		
		//Dialog.alert( Integer.toHexString( Keypad.key(keycode) ));
		
		switch (Keypad.key(keycode)) 
		{
        case Keypad.KEY_END:        	
            break;
        case Keypad.KEY_SEND: // 통화 키         	
        	
        	Ret = Call_SelectedNumber();

       		if( Ret == false )
       		{
       			Dialog.alert("Invalid Phone Number");
       		}
       		else
       		{
       			super.close();
       		}
        	return true;  
        	
        	// SMS        	
        case KEYCODE_S:
        	
        	Ret = SMS_SelectedNumber();


       		if( Ret == false )
       		{
       			Dialog.alert("Invalid Phone Number");
       		}
       		else
       		{
       			super.close();
       		}
        	break;

/*        	
        	// MMS       	
        case KEYCODE_M:
        	
        	Ret = MMS_SelectedNumber();


       		if( Ret == false )
       		{
       			Dialog.alert("Invalid Phone Number");
       		}
       		else
       		{
       			super.close();
       		}
        	break;        	
*/
        	
        	
        	// EMAIL 
        case KEYCODE_E:
        	
        	Ret = EMAIL_SelectedNumber();


       		if( Ret == false )
       		{
       			Dialog.alert("Invalid Email Address");
       		}
       		else
       		{
       			super.close();
       		}
        	break;        	
        	
        case Keypad.KEY_ENTER:
        	
        	
        	// 순수하게 엔터키만 눌렀을때 실행..
        	if( Keypad.status(keycode) == 0 )
        	{
	    		String OptStr = new String(); 
	    			
	    		OptStr = _Option_Model.Get_Field( Option_Model.PHONE_SCREEN_ENTER_FUNC );

	    		if( Phone_Lib.isEMAIL_Address(super.Get_SelectedListString()) )
	    		{
	    			OptStr = "E-Mail";
	    		}
	    		
	    		Ret = Action_FromString( OptStr );
	    		
	    		if( Ret ) 
	    		{
	    			super.close();
	    			return true;
	    		}
        	}
        	
        	break;        	
        }

		
		return  super.keyDown(keycode, time);
	}	
	
	protected boolean navigationClick(int status, int time)
	{
		String OptStr = new String();
			
		OptStr = _Option_Model.Get_Field( Option_Model.PHONE_SCREEN_CLICK_FUNC );
		
		if( Phone_Lib.isEMAIL_Address(super.Get_SelectedListString()) )
		{
			OptStr = "E-Mail";
		}
		
		boolean Ret = Action_FromString( OptStr );
			
		if( Ret )
		{
			super.close();
			return true;
		}
		else
			return super.navigationClick(status, time);
	}	
	
    public boolean Call_SelectedNumber()
    {
    	int SelectedIndex = Get_SelectedListIndex();
    	boolean Ret = false;

    	if( SelectedIndex >= 0 )
    	{
    		Ret = Phone_Lib.Call_ToNumber( super.Get_SelectedListString() );
    		
    		if( Ret == true )
    		{
    			_Action = true;
    			_AfterCalling = true;
    		}
    	}
    	
    	return Ret;
    }
    
    public boolean SMS_SelectedNumber()
    {
    	int SelectedIndex = Get_SelectedListIndex();
    	boolean Ret = false;

    	if( SelectedIndex >= 0 )
    	{
    		Ret = Phone_Lib.SMS_ToNumber( super.Get_SelectedListString() );
    		
    		if( Ret == true )
    		{
    			_Action = true;
    		}
    	}
    	
    	return Ret;
    }
    
    public boolean MMS_SelectedNumber()
    {
    	int SelectedIndex = Get_SelectedListIndex();
    	boolean Ret = false;

    	if( SelectedIndex >= 0 )
    	{
    		Ret = Phone_Lib.MMS_ToNumber( super.Get_SelectedListString() );
    		
    		if( Ret == true )
    		{
    			_Action = true;
    		}
    	}
    	
    	return Ret;
    }
    
    public boolean EMAIL_SelectedNumber()
    {
    	int SelectedIndex = Get_SelectedListIndex();
    	boolean Ret = false;

    	if( SelectedIndex >= 0 )
    	{
    		Ret = Phone_Lib.EMAIL_ToAddress( super.Get_SelectedListString(), "" );
    		
    		if( Ret == true )
    		{
    			_Action = true;
    		}
    	}
    	
    	return Ret;
    }    
    
    

	protected boolean Action_FromString( String OptStr )
	{
		boolean Ret = false;
		
		if( OptStr.equals( "CALL" ) )
		{
	    	if( Call_SelectedNumber() == false )
	    	{
	    		Dialog.alert("Invalid Phone Number"); 
	    	}
	    	else
	    	{
	    		UiApplication.getUiApplication().requestBackground(); 
	    		return true;
	    	}
		}
		
		if( OptStr.equals( "SMS" ) )
		{
	    	if( SMS_SelectedNumber() == false )
	    	{
	    		Dialog.alert("Invalid Phone Number");    	
	    	}
	    	else
	    	{
	    		//UiApplication.getUiApplication().requestBackground(); 
	    		return true;
	    	}
		}		
		
		if( OptStr.equals( "MMS" ) )
		{
	    	if( SMS_SelectedNumber() == false )
	    	{
	    		Dialog.alert("Invalid Phone Number");    	
	    	}
	    	else
	    	{
	    		//UiApplication.getUiApplication().requestBackground(); 
	    		return true;
	    	}
		}			
		
		if( OptStr.equals( "E-Mail" ) )
		{
	    	if( EMAIL_SelectedNumber() == false )
	    	{
	    		Dialog.alert("Invalid E-Mail Address");    
	    	}
	    	else
	    	{
	    		//UiApplication.getUiApplication().requestBackground(); 
	    		
	    		_AfterSMS = true;
	    		return true;
	    	}
		}		
		
		return Ret;
	}	
	
	
    protected boolean navigationMovement(int dx, int dy, int status, int time )
    {
    	
    	// LEFT
    	if( dx < 0  )
    	{
    		close();
    	}
    	else
    	if( dx > 0 )
    	{
    		
    	}
		
    	return super.navigationMovement(dx, dy, status, time);
    }

}
